package com.material.controller.employee;


import com.material.common.entity.ResultResponse;
import com.material.common.utils.JsonUtils;
import com.material.common.utils.bean.BeanUtil;
import com.material.common.utils.poi.ExcelImportUtil;
import com.material.pound.consts.PoundState;
import com.material.pound.domain.FuelPound;
import com.material.pound.params.FuelPoundImportParams;
import com.material.pound.service.IFuelPoundService;
import com.material.request.AssayResultRequest;
import com.material.request.BackSkinRequest;
import com.material.request.FillPoundRequest;
import com.material.request.UploadQualityRequest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * @author ganxinge
 */
@Api(tags = "员工端磅单信息")
@RestController
@RequestMapping("/employee/pound")
public class EmployeePoundController {

    @Autowired
    private IFuelPoundService fuelPoundService;

    @ApiOperation("填写过磅信息")
    @PostMapping("/fillPound")
    public ResultResponse fillPound(@RequestBody FillPoundRequest fillPoundRequest){

        FuelPound fuelPound = BeanUtil.copyProperties(fillPoundRequest, FuelPound.class);
        JsonUtils.obj2Json(fillPoundRequest.getPoundPic());
        fuelPound.setWeighTime(new Date());
        fuelPound.setState(PoundState.STATE_1);
        fuelPound.setPoundPic(JsonUtils.obj2Json(fillPoundRequest.getPoundPic()));
        fuelPoundService.updateFuelPound(fuelPound);
        return ResultResponse.success();
    }
    @ApiOperation("原料入库确认")
    @GetMapping("/store/{id}")
    public ResultResponse store(@PathVariable("id") Long id){
        FuelPound fuelPound = new FuelPound();
        fuelPound.setState(PoundState.STATE_2);
        fuelPound.setId(id);
        fuelPound.setStoreTime(new Date());
        fuelPoundService.updateFuelPound(fuelPound);
        return ResultResponse.success();
    }

    @ApiOperation("填写回皮的皮重")
    @PostMapping("/backSkin")
    public ResultResponse backSkin(@RequestBody BackSkinRequest request){
        FuelPound fuelPound = BeanUtil.copyProperties(request, FuelPound.class);
        fuelPound.setState(PoundState.STATE_3);
        fuelPoundService.backSkin(fuelPound);
        return ResultResponse.success();
    }

    @ApiOperation("上传质检信息")
    @PostMapping("/uploadQuality")
    public ResultResponse uploadQuality(@RequestBody UploadQualityRequest uploadQualityRequest){
        FuelPound fuelPound = BeanUtil.copyProperties(uploadQualityRequest, FuelPound.class);
        fuelPound.setState(PoundState.STATE_4);
        fuelPound.setQualityPic(JsonUtils.obj2Json(uploadQualityRequest.getQualityPic()));
        fuelPoundService.uploadQuality(fuelPound);
        return ResultResponse.success();
    }
    @ApiOperation("化验结果录入")
    @PostMapping("/assayResult")
    public ResultResponse assayResult(@RequestBody AssayResultRequest assayResultRequest){
        FuelPound fuelPound = new FuelPound();
        fuelPound.setId(assayResultRequest.getId());
        fuelPound.setAssayResult(assayResultRequest.getAssayResult());
        fuelPound.setState(PoundState.STATE_5);
        fuelPoundService.assayResult(fuelPound);
        return ResultResponse.success();
    }
    @ApiOperation("化验结果上传")
    @PostMapping("/exportAssay")
    public ResultResponse exportAssay(MultipartFile file){
        List<FuelPoundImportParams> fuelPoundImportParams = null;
        try {
            fuelPoundImportParams = ExcelImportUtil.importExcel(file.getInputStream());
        } catch (IOException e) {
            return ResultResponse.error("导入异常");
        }
        String message = fuelPoundService.importAssay(fuelPoundImportParams);
        return ResultResponse.success(message);
    }


}
